package com.ruoyi.kenuo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kenuo.domain.KernelUserEqu;
import com.ruoyi.kenuo.service.IKernelUserEquService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户和设备的关系Controller
 *
 * @author polo
 * @date 2022-10-17
 */
@RestController
@RequestMapping("/kenuo/userEqu")
public class KernelUserEquController extends BaseController
{
    @Autowired
    private IKernelUserEquService kernelUserEquService;

    /**
     * 查询用户和设备的关系列表
     */
    @GetMapping("/list")
    public TableDataInfo list(KernelUserEqu kernelUserEqu)
    {
        startPage();
        List<KernelUserEqu> list = kernelUserEquService.selectKernelUserEquList(kernelUserEqu);
        return getDataTable(list);
    }

    /**
     * 根据手机号/用户id、sn码查询 设备和手机号绑定的关系
     * @return
     */
    @GetMapping("/getUserEqu")
    public AjaxResult getUserEqu(KernelUserEqu kernelUserEqu){
        if(ObjectUtil.isEmpty(kernelUserEqu)){
            return AjaxResult.error("参数不能为空");
        }
        AjaxResult  ar = kernelUserEquService.getUserEqu(kernelUserEqu);
        return ar;
    }
    /**
     * 根据账号查询绑定关系
     * @return
     */
    @GetMapping("/getUserEquByZh")
    public AjaxResult getUserEquByZh(KernelUserEqu kernelUserEqu){
        if(StrUtil.isEmpty(kernelUserEqu.getRemarkOne())){
            return AjaxResult.error("参数remarkOne不能为空");
        }
        AjaxResult  ar = kernelUserEquService.getUserEquByZh(kernelUserEqu);
        return ar;
    }
    /**
     * 导出用户和设备的关系列表
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:equ:export')")
    @Log(title = "用户和设备的关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KernelUserEqu kernelUserEqu)
    {
        List<KernelUserEqu> list = kernelUserEquService.selectKernelUserEquList(kernelUserEqu);
        ExcelUtil<KernelUserEqu> util = new ExcelUtil<KernelUserEqu>(KernelUserEqu.class);
        util.exportExcel(response, list, "用户和设备的关系数据");
    }

    /**
     * 获取用户和设备的关系详细信息
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:equ:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(kernelUserEquService.selectKernelUserEquById(id));
    }

    /**
     * 新增用户和设备的关系
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:equ:add')")
    @Log(title = "用户和设备的关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KernelUserEqu kernelUserEqu)
    {
        int mt = kernelUserEquService.insertKernelUserEqu(kernelUserEqu);
        if(mt==11){
            return AjaxResult.error("此设备已被绑定");
        }
        return toAjax(kernelUserEquService.insertKernelUserEqu(kernelUserEqu));
    }

    /**
     * 修改用户和设备的关系
     */
    @PreAuthorize("@ss.hasPermi('kenuo:equ:edit')")
    @Log(title = "用户和设备的关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KernelUserEqu kernelUserEqu)
    {
        return toAjax(kernelUserEquService.updateKernelUserEqu(kernelUserEqu));
    }

    /**
     * 删除用户和设备的关系
     */
    @PreAuthorize("@ss.hasPermi('kenuo:equ:remove')")
    @Log(title = "用户和设备的关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(kernelUserEquService.deleteKernelUserEquByIds(ids));
    }

    /**
     * 解绑
     * @param snCode
     * @param tel
     * @return
     */
    @GetMapping("/removeEquUser")
    public AjaxResult removeEquUser(String snCode,String tel)
    {
        if(StrUtil.isEmpty(snCode)||StrUtil.isEmpty(tel)){
            return AjaxResult.error("参数缺失");
        }
        AjaxResult ar = kernelUserEquService.removeEquUser(snCode,tel);
        return ar;
    }
}
